package syt.control;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import syt.action.ChatService;
import syt.action.User;

@Controller
@RequestMapping("/action")
public class MessageAction {
	@Autowired
	private ChatService chatService;
	  
		@RequestMapping(value="/login_action", method=RequestMethod.POST)
		public String ChatLoginAction(HttpServletRequest request,int aaa) {
			try {
				request.setCharacterEncoding("UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String name = request.getParameter("name");
			User user = new User(name);
			chatService.login(user);
			request.getSession().setAttribute("sessionname",user);
			return "syt_index";	
		}
		

		@RequestMapping(value="/listen",method=RequestMethod.POST)
		public String listenAction(HttpServletRequest request) {
			User user = (User)request.getSession().getAttribute("sessionname");
			String mp3URL = request.getParameter("mp3URL");
			String title = request.getParameter("title");
			chatService.preListenMp3Action(user.getName(),title,mp3URL);
			return "ok";
		}

		
		
		@RequestMapping(value="/getmessage",method=RequestMethod.GET)
		public String getMessage(HttpServletRequest request,Model model) {
			User user = (User)request.getSession().getAttribute("sessionname");
			model.addAttribute("messageList", chatService.getAvailableMessages(user));
			return "ok";
		}

		@RequestMapping(value="/getName",method=RequestMethod.GET)
		public String getName(HttpServletRequest request,Model model) {
			User user = (User)request.getSession().getAttribute("sessionname");
			model.addAttribute("user", user);
			return "ok";
		}

		
		/**
		 * @param chatService the chatService to set
		 */
		public void setChatService(ChatService chatService) {
			this.chatService = chatService;
		}
	  

}
